package cn.iocoder.yudao.module.mqtt.controller.admin.configalarm;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.configalarm.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.configalarm.ConfigAlarmDO;
import cn.iocoder.yudao.module.mqtt.service.configalarm.ConfigAlarmService;

@Tag(name = "管理后台 - MQTT报警配置")
@RestController
@RequestMapping("/mqtt/config-alarm")
@Validated
public class ConfigAlarmController {

    @Resource
    private ConfigAlarmService configAlarmService;

    @PostMapping("/create")
    @Operation(summary = "创建MQTT报警配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:create')")
    public CommonResult<Long> createConfigAlarm(@Valid @RequestBody ConfigAlarmSaveReqVO createReqVO) {
        return success(configAlarmService.createConfigAlarm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MQTT报警配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:update')")
    public CommonResult<Boolean> updateConfigAlarm(@Valid @RequestBody ConfigAlarmSaveReqVO updateReqVO) {
        configAlarmService.updateConfigAlarm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MQTT报警配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:delete')")
    public CommonResult<Boolean> deleteConfigAlarm(@RequestParam("id") Long id) {
        configAlarmService.deleteConfigAlarm(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除MQTT报警配置")
                @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:delete')")
    public CommonResult<Boolean> deleteConfigAlarmList(@RequestParam("ids") List<Long> ids) {
        configAlarmService.deleteConfigAlarmListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MQTT报警配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:query')")
    public CommonResult<ConfigAlarmRespVO> getConfigAlarm(@RequestParam("id") Long id) {
        ConfigAlarmDO configAlarm = configAlarmService.getConfigAlarm(id);
        return success(BeanUtils.toBean(configAlarm, ConfigAlarmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MQTT报警配置分页")
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:query')")
    public CommonResult<PageResult<ConfigAlarmRespVO>> getConfigAlarmPage(@Valid ConfigAlarmPageReqVO pageReqVO) {
        PageResult<ConfigAlarmDO> pageResult = configAlarmService.getConfigAlarmPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigAlarmRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MQTT报警配置 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:config-alarm:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigAlarmExcel(@Valid ConfigAlarmPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigAlarmDO> list = configAlarmService.getConfigAlarmPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MQTT报警配置.xls", "数据", ConfigAlarmRespVO.class,
                        BeanUtils.toBean(list, ConfigAlarmRespVO.class));
    }

}